package com.etrans.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

	private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	/**
	 * 获取当月月初时间
	 * 
	 * @return
	 */
	public static String getMinMonthDate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(System.currentTimeMillis()));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}
	
	public static Long getTwoWeekAgoTime(){
		Long now = System.currentTimeMillis();
		return (now-14*24*3600*1000)/1000;
	}
	
	
	 /**
     * 返回指定日期的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.MONTH), 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
	
	
	/**
	 * 获取当前的系统时间，返回String 类型
	 * 
	 * @return
	 */
	public static String getSysTimeStr() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = df.format(new Date());
		return time;
	}

	/**
	 * 从系统时间减去间隔的时间
	 * 
	 * 间隔时间单位为：天
	 * 
	 * 格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param interval
	 * @return
	 */
	public static String sysStartDate(int interval) {
		Date startdate = tommorowDate(interval - 1);
		String startString = DateCoventString(startdate);
		return startString;
	}

	/**
	 * 获取系统第二天凌晨日期，返回long
	 * 
	 * @return
	 */
	public static Date tommorowDate(int interval) {
		Calendar curDate = Calendar.getInstance();
		Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH),
				curDate.get(Calendar.DATE) - interval, 0, 0, 0);
		Date beforeDay = tommorowDate.getTime();
		return beforeDay;
	}

	/**
	 * 获取系统凌晨日期，返回Date
	 * 
	 * @return
	 */
	public static Date currentDate() {
		Calendar curDate = Calendar.getInstance();
		Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH),
				curDate.get(Calendar.DATE), 0, 0, 0);
		Date afterdays = tommorowDate.getTime();
		return afterdays;
	}

	/**
	 * date 转成String 类型，格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String DateCoventString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = df.format(date);
		return time;
	}
	
	/**
	 * date 转成String 类型，格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String strCoventDate(String dateStr) {
		try{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time = df.parse(dateStr);
			return sdf.format(time);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取系统时间23：59：59时间，类型为yyyy-MM-dd HH:mm:ss
	 * 
	 * @param day
	 * @return
	 */
	public static String currentDay() {
		Calendar curDate = Calendar.getInstance();
		Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH),
				curDate.get(Calendar.DATE), 23, 59, 59);
		String currentDay = DateCoventString(tommorowDate.getTime());
		return currentDay;

	}
	
	public static String getOuterSyncDate() {
		Calendar curDate = Calendar.getInstance();
		return df.format(curDate.getTime());
	}
	
	public static Long getDateFromString(String dateStr) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
			date = sdf.parse(dateStr);
		return date.getTime();
	}
	
	public static void main(String[] args){
		System.out.println(getBeforeThreeMonth("yyyy-MM-dd"));
	}
	
	/**
	 * 获取当前月第一天的前三个月的时间
	 * @return
	 */
	public static String getBeforeThreeMonth(String pattern){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 0);  
		calendar.set(Calendar.DAY_OF_MONTH, 1); 
		calendar.add(Calendar.MONTH, -2);    //得到前2个月间
		Date date = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * 提前几个月
	 * @param pattern
	 * @param month
	 * @return
	 */
	public static String getBeforeMonth(String pattern,int month){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -month);    //得到前2个月间
		Date date = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

}
